﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Business.Stores;
using MyCommerce.Business.GeneralParameters;

namespace MyCommerce.Presentation.Stores.Presenters
{
    public class SupplierPresenter
    {
        private IViews.ISupplier vManagedView;

        public SupplierPresenter(IViews.ISupplier managedView)
        {
            if (managedView == null)
            {
                throw new Exception("managedView cannot be null");
            }

            vManagedView = managedView;
            vManagedView.OnLoad += new EventHandler(vManagedView_OnLoad);
            vManagedView.OnEdit += new IViews.OnEntityEventHandler(vManagedView_OnEdit);
            vManagedView.OnDelete += new IViews.OnEntityEventHandler(vManagedView_OnDelete);
            vManagedView.OnSave += new IViews.OnSaveEventHandler(vManagedView_OnSave);

        }

        void vManagedView_OnSave(object sender, bool isNew)
        {
            SupplierManager vSupplierManager = new SupplierManager();
            if (isNew)
            {
                vSupplierManager.SaveSupplier(vManagedView.objSupplier);
            }
            else
            {
                vSupplierManager.UpdateSupplier(vManagedView.objSupplier);
            }
            vManagedView.colSupplier = vSupplierManager.GetSuppliers();
        }

        void vManagedView_OnDelete(object sender, long id)
        {
            SupplierManager vSupplierManager = new SupplierManager();
            vSupplierManager.DeleteSupplier(id);
            vManagedView.colSupplier = vSupplierManager.GetSuppliers();
        }

        void vManagedView_OnEdit(object sender, long id)
        {
            SupplierManager vSupplierManager = new SupplierManager();
            vManagedView.objSupplier = vSupplierManager.GetSupplierByID(id);
        }

        void vManagedView_OnLoad(object sender, EventArgs e)
        {
            SupplierManager vSupplierManager = new SupplierManager();
            CountryManager vCountryManager = new CountryManager();

            vManagedView.colSupplier = vSupplierManager.GetSuppliers();
            vManagedView.colCountry = vCountryManager.GetListCountries();
        }

    }
}
